 using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using MedicService.Domain.Generic;
using MedicService.Domain.Print;
using MedicService.Domain.Print.Template;
using MedicService.Server.Factory.Generic;

namespace MedicService.Server.Factory.Print {

    public class PrintTemplateFactory<T> : EntityFactory<T> where T : PrintTemplate, new(){

        public PrintTemplateFactory() : base("PrintTemplate", "id", "Type='" + typeof(T).GetGenericArguments()[0]+"'") { }

        public PrintTemplateFactory(String type) : base("PrintTemplate", "id", "Type='" + type+"'") { }
        
        protected override T CreateEntity() {
            return new T();
        }

        protected override string GetInsertQuery(T entity) {
            string q = "";
            if (entity.Active) {
                q += "UPDATE [PrintTemplate] SET [Active] = 0 WHERE Type = " + Nullable(entity.Type) + ";";
            }
            q += "INSERT INTO [PrintTemplate]"
                       + "           ([Mode]"
                       + "           ,[Format]"
                       + "           ,[MarginLeft]"
                       + "           ,[MarginRight]"
                       + "           ,[MarginTop]"
                       + "           ,[MarginBottom]"
                       + "           ,[MarginHeader]"
                       + "           ,[MarginFooter]"
                       + "           ,[Orientation]"
                       + "           ,[Template]"
                       + "           ,[Title]"
                       + "           ,[Active]"
                       + "           ,[Type])"
                       + "     VALUES"
                       + "           (" + Nullable(entity.Mode)
                       + "           ," + Nullable(entity.Format)
                       + "           ," + entity.MarginLeft.ToString().Replace(",",".")
                       + "           ," + entity.MarginRight.ToString().Replace(",", ".")
                       + "           ," + entity.MarginTop.ToString().Replace(",", ".")
                       + "           ," + entity.MarginBottom.ToString().Replace(",", ".")
                       + "           ," + entity.MarginHeader.ToString().Replace(",", ".")
                       + "           ," + entity.MarginFooter.ToString().Replace(",", ".")
                       + "           ," + Nullable(entity.Orientation)
                       + "           ," + NotNullString(entity.Template)
                       + "           ," + NotNullString(entity.Title)
                       + "           ," + (entity.Active ? "1" : "0")
                       + "           ," + Nullable(entity.Type) + ")";
            return q;
        }

        protected override string GetUpdateQuery(T entity) {
            string q = "";
            if (entity.Active) {
                q += "UPDATE [PrintTemplate] SET [Active] = 0 WHERE Type = " + Nullable(entity.Type) + ";";
            }
            q += "UPDATE [PrintTemplate]"
                       + "   SET [Mode] = " + Nullable(entity.Mode)
                       + "      ,[Format] = " + Nullable(entity.Format)
                       + "      ,[MarginLeft] = " + entity.MarginLeft.ToString().Replace(",", ".")
                       + "      ,[MarginRight] = " + entity.MarginRight.ToString().Replace(",", ".")
                       + "      ,[MarginTop] = " + entity.MarginTop.ToString().Replace(",", ".")
                       + "      ,[MarginBottom] = " + entity.MarginBottom.ToString().Replace(",", ".")
                       + "      ,[MarginHeader] = " + entity.MarginHeader.ToString().Replace(",", ".")
                       + "      ,[MarginFooter] = " + entity.MarginFooter.ToString().Replace(",", ".")
                       + "      ,[Orientation] = " + Nullable(entity.Orientation)
                       + "      ,[Template] = " + NotNullString(entity.Template)
                       + "      ,[Title] = " + NotNullString(entity.Title)
                       + "      ,[Active] = " + (entity.Active ? "1" : "0")
                       + "      ,[Type] = " + Nullable(entity.Type)
                       + " WHERE id=" + entity.Id;
            return q;
        }

        protected float toFloat(object o) {
            string s = "" + o;
            return float.Parse(s);
        }

        protected override void FillEntity(T entity, DataRow myDataRow) {
            base.FillEntity(entity, myDataRow);
            entity.Mode = (string)myDataRow["Mode"];
            entity.Format = (string)myDataRow["Format"];
            entity.MarginLeft = toFloat(myDataRow["MarginLeft"]);
            entity.MarginRight = toFloat(myDataRow["MarginRight"]);
            entity.MarginTop = toFloat(myDataRow["MarginTop"]);
            entity.MarginBottom = toFloat(myDataRow["MarginBottom"]);
            entity.MarginHeader = toFloat(myDataRow["MarginHeader"]);
            entity.MarginFooter = toFloat(myDataRow["MarginFooter"]);
            entity.Orientation = (string)myDataRow["Orientation"];
            entity.Template = (string)myDataRow["Template"];
            entity.Title = (string)myDataRow["Title"];
            entity.Type = (string)myDataRow["Type"];
            string s = "" + myDataRow["Active"];
            entity.Active = (s == null || s == "0") ? false : true;

        }

    }
}